package com.agileboot.admin.controller.medicine;

import cn.hutool.core.lang.Assert;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.utils.StringTool;
import com.agileboot.domain.feedback.db.IUserFeedbackService;
import com.agileboot.domain.feedback.db.UserFeedback;
import com.agileboot.domain.feedback.dto.UserFeedbackReq;
import com.agileboot.domain.feedback.vo.UserFeedbackVO;
import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.db.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户意见反馈
 * </p>
 *
 * @author lx
 * @since 2023-12-08
 */
@RestController
@RequestMapping("/feedback")
@Api(value = "后台-反馈", tags = "后台-反馈")
public class FeedBackController {

    @Autowired
    private IUserFeedbackService userFeedbackService;

    @Autowired
    private SysUserService userService;

    @GetMapping("/page")
    @ApiOperation(value = "反馈分页列表", notes = "反馈分页列表")
    public ResponseDTO pageFeedback(@ApiParam("pageSize") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                    @ApiParam("pageNum") @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @ApiParam("反馈信息状态") @RequestParam(value = "status", required = false) Integer status,
                                    @ApiParam("反馈信息名") @RequestParam(value = "userName", required = false) String userName
    ) {
        //设置分页对象
        Page<UserFeedback> paramPage = new Page<>();
        paramPage.setSize(pageSize);
        paramPage.setCurrent(pageNum);
        paramPage.addOrder(OrderItem.desc("create_time"));
        //设置查询条件
        QueryWrapper<UserFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("status");
        queryWrapper.orderByDesc("create_time");
        //加入搜索条件
        if (status != null) {
            queryWrapper.lambda().eq(UserFeedback::getStatus, status);
        }
        if (!StringUtils.isBlank(userName)) {
            queryWrapper.lambda().like(UserFeedback::getUserName,userName);
        }
        //获得数据
        Page resPage = userFeedbackService.page(paramPage, queryWrapper);

        List<UserFeedback> list = resPage.getRecords();
        //判空
        if (CollectionUtils.isEmpty(resPage.getRecords()))return ResponseDTO.ok(resPage);
        //填充数据
        List<UserFeedbackVO> collect = list.stream().map(userFeedback -> {
            UserFeedbackVO userFeedbackVO = UserFeedbackVO.of(userFeedback);
            SysUserEntity user = userService.getById(userFeedback.getUserId());
            userFeedbackVO.setPhone(user.getPhoneNumber());

            return userFeedbackVO;
        }).collect(Collectors.toList());

        resPage.setRecords(collect);

        return ResponseDTO.ok(resPage);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "反馈详情", notes = "反馈详情")
    public ResponseDTO detail(@ApiParam(value = "反馈信息ID",required = true) @RequestParam("id") Long id) {

        UserFeedback one = userFeedbackService.getById(id);
        if (one == null) {
            return ResponseDTO.fail("id不存在");
        }
        UserFeedbackVO userFeedbackVO = UserFeedbackVO.of(one);
        SysUserEntity user = userService.getById(one.getUserId());
        userFeedbackVO.setPhone(user.getPhoneNumber());

        return ResponseDTO.ok(userFeedbackVO);

    }

    @PostMapping("/handle")
    @ApiOperation(value = "处理反馈信息", notes = "处理反馈信息")
    public ResponseDTO handle(@RequestBody UserFeedbackReq req) {
        if (req.getId() == null) {
            return ResponseDTO.fail("参数为空");
        }
        if (StringUtils.isBlank(req.getHandleContent()) && StringUtils.isBlank(req.getHandleImg())){
            return ResponseDTO.fail("处理内容不能为空");
        }
        UserFeedback userFeedback = new UserFeedback();
        userFeedback.setId(req.getId());
        userFeedback.setHandleContent(req.getHandleContent());
        userFeedback.setHandleImg(req.getHandleImg());
        userFeedback.setHandleTime(LocalDateTime.now());
        userFeedback.setStatus(1);
        userFeedbackService.updateById(userFeedback);
        return ResponseDTO.ok("修改成功");

    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除反馈信息", notes = "删除反馈信息")
    public ResponseDTO delete(@ApiParam(value = "反馈信息ID",required = true) @RequestParam("ids") String ids) {
        Assert.isTrue(StringUtils.isNotBlank(ids),"请求参数为空");
        List<Long> list = StringTool.toLongList(",", ids);
        userFeedbackService.removeByIds(list);
        return ResponseDTO.ok("删除成功");

    }
}
